'use strict';

/* Services */

var appServices = angular.module('appServices', []);

appServices.factory('sessionInjector', [ '$q', 
		function($q) {
			var sessionInjector = {
				request : function(config) {
					var user = Session.loadObject("user");
					if (user != null) {
						config.headers['sessionid'] = user.sessionId;
						// config.headers['x-session-username'] = user.username;
					}

					return config;
				}
			};
			return sessionInjector;
		} ]);
appServices.factory('Loading', [ '$ionicLoading', function($ionicLoading) {
	return {
		show : function() {
			$ionicLoading.show({

				content : 'Loading',
				animation : 'fade-in'
			});
		},
		hide : function() {
			$ionicLoading.hide();
		}
	}
} ]);
appServices.config([ '$httpProvider', function($httpProvider) {
	$httpProvider.interceptors.push('sessionInjector');
} ]);

appServices.factory('Camera', ['$q', function($q){
  return {
    getPicture: function(n){
      var q = $q.defer();
      var options = new Object();
      if(n==2){options.sourceType = "Camera.PictureSourceType.PHOTOLIBRARY";}
      navigator.camera.getPicture(function(result){
        q.resolve(result);
      }, function(err){
        q.reject(err);
      }, options);
      return q.promise;
    }
  };
}])
